package com.vanvalt.payhui.modules.pay.api.business;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.modules.sys.entity.Office;
import com.thinkgem.jeesite.modules.sys.entity.Role;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.service.OfficeService;
import com.thinkgem.jeesite.modules.sys.service.SystemService;
import com.vanvalt.payhui.modules.pay.api.command.impl.RequestCommand;
import com.vanvalt.payhui.modules.pay.api.command.impl.ResponseCommand;
import com.vanvalt.payhui.modules.pay.api.command.impl.UserCommand;
import com.vanvalt.payhui.modules.pay.api.util.ErrorCode;
import com.vanvalt.payhui.modules.pay.entity.UserShare;
import com.vanvalt.payhui.modules.pay.service.BankCardService;
import com.vanvalt.payhui.modules.pay.service.ProfitService;
import com.vanvalt.payhui.modules.pay.service.UserService;
import com.vanvalt.payhui.modules.pay.service.UserShareService;
import com.vanvalt.payhui.util.constat.Constant;
import com.vanvalt.payhui.util.encrypt.DesUtil;
import com.vanvalt.payhui.util.json.JsonConversion;
import com.vanvalt.payhui.util.mob.sms.spi.SmsVerifyKit;
import com.vanvalt.payhui.util.random.ShareCode;


/**
 * 用户处理
 *
 */
public class UserProcess extends BaseProcess {
	/**
	 *日志.
	 */
	private static Logger 				logger = Logger.getLogger(UserProcess.class);
	private static HttpServletRequest	httpServletRequest	= ((ServletRequestAttributes) RequestContextHolder
			.getRequestAttributes()).getRequest();
	
	/**
	 * 系统用户
	 */
	private UserService userService;
	private SystemService systemService;
	private OfficeService officeService;
	private UserShareService userShareService;
	private ProfitService profitService;
	private BankCardService bankCardService;
	
	/**
	 * 构造方法.
	 */	
	public UserProcess() {
		super();
	}

	/**
	 * @param userService
	 */
	public UserProcess(UserService userService) {
		super();
		this.userService = userService;
	}
	
	/**
	 * @param userService
	 * @param systemService
	 */
	public UserProcess(UserService userService, SystemService systemService) {
		super();
		this.userService = userService;
		this.systemService = systemService;
	}
	
	/**
	 * @param userService
	 * @param systemService
	 * @param officeService
	 */
	public UserProcess(UserService userService, SystemService systemService, OfficeService officeService) {
		super();
		this.userService = userService;
		this.systemService = systemService;
		this.officeService = officeService;
	}

	/**
	 * @param userService
	 * @param profitService
	 * @param userShareService
	 * @param bankCardService
	 */
	public UserProcess(UserService userService, ProfitService profitService, UserShareService userShareService, BankCardService bankCardService) {
		super();
		this.userService = userService;
		this.profitService = profitService;
		this.userShareService = userShareService;
		this.bankCardService = bankCardService;
	}
	
	/**
	 * 用户登录。
	 * 
	 * @param requestCommand
	 * @param responseCommand
	 * @return
	 * @throws IOException
	 * @throws JsonMappingException
	 * @throws JsonParseException
	 */
	public ResponseCommand login(RequestCommand requestCommand, ResponseCommand responseCommand)
			throws JsonParseException, JsonMappingException, IOException {

		UserCommand userCommand = new UserCommand(requestCommand.getObject().toString());
		Map<String, Object> map = userCommand.getMap();
		String userName = (String) map.get("userName");
        String password = (String) map.get("password");
        
        Map<String,Object> resultMap = Maps.newHashMap();
        
        if(map.isEmpty() && StringUtils.isBlank(userName) && StringUtils.isBlank(password)){
            return returnResult(ErrorCode.ERR_CODE_STATUS_FALSE, ErrorCode.ERR_CODE_PRECONDITION_FAILED, responseCommand);
        } else {
        	
        	User userParam = new User();
        	userParam.setLoginName(userName);
        	userParam.setDelFlag(Constant.DEL_FLAG_NORMAL);
        	
        	List<User> userList = userService.findList(userParam);
        	
        	if(userList == null || userList.size() == 0){ // 用户不存在
        		return returnResult(ErrorCode.ERR_CODE_STATUS_FALSE, ErrorCode.ERR_CODE_PRECONDITION_FAILED_USER_NOT_EXIST, responseCommand);
        	} else {
        		User user = userList.get(0);
        		try {
        			// 明文密码(通过DES解密)
					String plainPassword = DesUtil.decrypt(password, Constant.PASSWORD_DES_KEY);
					// SHA1加密
					//String sha1Password = systemService.entryptPassword(plainPassword);
					if(!systemService.validatePassword(plainPassword, user.getPassword())){ // 密码校验不通过
						return returnResult(ErrorCode.ERR_CODE_STATUS_FALSE, ErrorCode.ERR_CODE_PRECONDITION_FAILED_PASSWORD_ERROR, responseCommand);
					} else {
	        			
	        			resultMap.put("uid", user.getId());
	        			resultMap.put("userName", user.getLoginName());
	        			resultMap.put("name", user.getName());
	        			resultMap.put("lastestLoginTime", user.getLoginDate());
	        			resultMap.put("inviteCode", user.getInviteCode()); // 邀请码
	        			resultMap.put("photo", user.getPhoto()); // 个人头像
	        			// 更新登录时间
	        			userService.updateUserLoginInfo(user);
	        		}
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					return returnResult(ErrorCode.ERR_CODE_STATUS_FALSE, ErrorCode.ERR_CODE_PRECONDITION_FAILED_PASSWORD_ERROR, responseCommand);
				}
        	}
        }
		
        return returnResult(ErrorCode.ERR_CODE_STATUS_TRUE, ErrorCode.ERR_CODE_SUCCESS, 
 				JsonConversion.writeMapJSON(resultMap), responseCommand);
	}
	
	/**
	 * 修改密码
	 * @param requestCommand
	 * @param responseCommand
	 * @return
	 * @throws JsonParseException
	 * @throws JsonMappingException
	 * @throws IOException
	 */
	public ResponseCommand changePwd(RequestCommand requestCommand, ResponseCommand responseCommand)
			throws JsonParseException, JsonMappingException, IOException {

		UserCommand userCommand = new UserCommand(requestCommand.getObject().toString());
		Map<String, Object> map = userCommand.getMap();
		String uid = (String) map.get("uid");
        String oldPassword = (String) map.get("oldPassword");
        String newPassword = (String) map.get("newPassword");
        
        Map<String,Object> resultMap = Maps.newHashMap();
        
        if(map.isEmpty() && StringUtils.isBlank(uid) && StringUtils.isBlank(oldPassword) && StringUtils.isBlank(newPassword)){
            return returnResult(ErrorCode.ERR_CODE_STATUS_FALSE, ErrorCode.ERR_CODE_PRECONDITION_FAILED, responseCommand);
        } else {
        	
        	if(uid != null && !"".equals(uid)){
        		User user = userService.get(uid);
        		
        		if(user == null){ // 用户不存在
        			return returnResult(ErrorCode.ERR_CODE_STATUS_FALSE, ErrorCode.ERR_CODE_PRECONDITION_FAILED_USER_NOT_EXIST, responseCommand);
        		} else {
        			
    				// 明文密码(通过DES解密)
					String plainOldPassword;
					String plainNewPassword;
					try {
						plainOldPassword = DesUtil.decrypt(oldPassword, Constant.PASSWORD_DES_KEY);
						plainNewPassword = DesUtil.decrypt(newPassword, Constant.PASSWORD_DES_KEY);
    					// SHA1加密旧密码
    					//String sha1Password = systemService.entryptPassword(plainOldPassword);
    					if(!systemService.validatePassword(plainOldPassword, user.getPassword())){ // 旧密码校验不通过
    						return returnResult(ErrorCode.ERR_CODE_STATUS_FALSE, ErrorCode.ERR_CODE_PRECONDITION_FAILED_PASSWORD_ERROR, responseCommand);
    					} else {
    						// 更新密码
        					systemService.updatePasswordById(uid, user.getLoginName(), plainNewPassword);
    					}
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
        		}
        	}
        }
        
        return returnResult(ErrorCode.ERR_CODE_STATUS_TRUE, ErrorCode.ERR_CODE_SUCCESS, 
 				JsonConversion.writeMapJSON(resultMap), responseCommand);
	}
	
	/**
	 * 用户注册
	 * @param requestCommand
	 * @param responseCommand
	 * @return
	 * @throws JsonParseException
	 * @throws JsonMappingException
	 * @throws IOException
	 * @throws JSONException 
	 */
	public ResponseCommand regist(RequestCommand requestCommand, ResponseCommand responseCommand)
			throws JsonParseException, JsonMappingException, IOException, JSONException {

		UserCommand userCommand = new UserCommand(requestCommand.getObject().toString());
		Map<String, Object> map = userCommand.getMap();
		String userName = (String) map.get("userName");
		String name = (String) map.get("name");
        String password = (String) map.get("password");
        String deviceType = (String) map.get("deviceType");
        String code = (String) map.get("code");
        
        Map<String,Object> resultMap = Maps.newHashMap();
        
        if(map.isEmpty() && StringUtils.isBlank(userName) && StringUtils.isBlank(name) && StringUtils.isBlank(password)
        		&& StringUtils.isBlank(deviceType) && StringUtils.isBlank(code)){
            return returnResult(ErrorCode.ERR_CODE_STATUS_FALSE, ErrorCode.ERR_CODE_PRECONDITION_FAILED, responseCommand);
        } else {
        	
        	if(userName != null && !"".equals(userName)){
        		// 根据登录名查询用户信息
        		User user = systemService.getUserByLoginName(userName);
        		
        		if(user != null){ // 用户已存在
        			return returnResult(ErrorCode.ERR_CODE_STATUS_FALSE, ErrorCode.ERR_CODE_PRECONDITION_FAILED_USER_EXISTING, responseCommand);
        		} else {
        			
        			String appKey = null;
        			if(deviceType != null && "1".equals(deviceType)){ // Android
        				appKey = Global.getConfig("mob.sms.android.appkey");
        			} else if("2".equals(deviceType)){ // IOS
        				appKey = Global.getConfig("mob.sms.ios.appkey");
        			}
        			
        			String result = null;
        			try {
        				SmsVerifyKit verifyKit = new SmsVerifyKit(appKey, userName, Constant.SMS_DEFAULT_ZONE, code);
    					result = verifyKit.go();
    				} catch (Exception e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				}
        			
        			if(result != null && !"".equals(result)){
        				JSONObject json = new JSONObject(result);
        				int status = (Integer) json.get("status");
        				
        				if(status == 200){
        					
        					// 明文密码(通过DES解密)
        					String plainPassword;
        					try { // fOaniLvyTTE=  111111
        						plainPassword = DesUtil.decrypt(password, Constant.PASSWORD_DES_KEY);
        						
        						User newUser = new User();
        						
        						newUser.setCompany(this.getCompany());
        						newUser.setOffice(this.getOffice());
        						newUser.setLoginName(userName);
        						newUser.setPassword(SystemService.entryptPassword(plainPassword));
        						newUser.setName(name);
        						newUser.setPhone(userName);
        						newUser.setMobile(userName);
        						newUser.setUserType(Constant.USER_TYPE_COMMON); // 普通用户
        						newUser.setNo(Constant.USER_NO_DEFAULT); // 默认用户工号
        						newUser.setRemarks("普通用户");
        						// 普通用户默认角色列表
        						List<Role> roleList = this.getRoleListByEn(Constant.USER_ROLE_DEFAULT);
        						newUser.setRoleList(roleList);
        						
        						User adUser = systemService.getUserByLoginName("admin");
        						newUser.setCreateBy(adUser);
        						newUser.setUpdateBy(adUser);
        						
        						// 保存用户信息
        						systemService.saveUser(newUser);
        						
        						int tid = newUser.getTid();
        						// 根据用户TID生成邀请码
        						String newUserInviteCode = ShareCode.toSerialCode(tid);
        						newUser.setInviteCode(newUserInviteCode.toUpperCase());
        						// 更新用户邀请码
        						systemService.updateUserInviteCode(newUser);
        						
        						return returnResult(ErrorCode.ERR_CODE_STATUS_TRUE, ErrorCode.ERR_CODE_SUCCESS, 
        				 				JsonConversion.writeMapJSON(resultMap), responseCommand);
        					} catch (Exception e) {
        						// TODO Auto-generated catch block
        						e.printStackTrace();
        					}
        				} else if(status == 468) {
        					//System.out.println(Constant.SMS_VERIFY_CODE_STATUS_MAP.get(status));
        					return returnResult(ErrorCode.ERR_CODE_STATUS_FALSE, ErrorCode.ERR_CODE_PRECONDITION_FAILED_VERIFY_CODE, responseCommand);
        				} else {
        					//System.out.println(Constant.SMS_VERIFY_CODE_STATUS_MAP.get(status));
        					return returnResult(ErrorCode.ERR_CODE_STATUS_FALSE, ErrorCode.ERR_CODE_PRECONDITION_FAILED_SMS_SERVICE, responseCommand);
        				}
        			}
        		}
        	}
        	
        	return returnResult(ErrorCode.ERR_CODE_STATUS_FALSE, ErrorCode.ERR_CODE_EXCEPTION, 
	 				JsonConversion.writeMapJSON(resultMap), responseCommand);
        }
	}
	
	/**
	 * 找回密码
	 * @param requestCommand
	 * @param responseCommand
	 * @return
	 * @throws JsonParseException
	 * @throws JsonMappingException
	 * @throws IOException
	 * @throws JSONException
	 */
	public ResponseCommand retrievePassword(RequestCommand requestCommand, ResponseCommand responseCommand)
			throws JsonParseException, JsonMappingException, IOException, JSONException {

		UserCommand userCommand = new UserCommand(requestCommand.getObject().toString());
		Map<String, Object> map = userCommand.getMap();
		String userName = (String) map.get("userName");
        String password = (String) map.get("password");
        String deviceType = (String) map.get("deviceType");
        String code = (String) map.get("code");
        
        Map<String,Object> resultMap = Maps.newHashMap();
        
        if(map.isEmpty() && StringUtils.isBlank(userName) && StringUtils.isBlank(deviceType) && StringUtils.isBlank(password)
        		&& StringUtils.isBlank(code)){
            return returnResult(ErrorCode.ERR_CODE_STATUS_FALSE, ErrorCode.ERR_CODE_PRECONDITION_FAILED, responseCommand);
        } else {
        	
        	if(userName != null && !"".equals(userName)){
        		// 根据登录名查询用户信息
        		User user = systemService.getUserByLoginName(userName);
        		
        		if(user == null){ // 用户不存在
        			return returnResult(ErrorCode.ERR_CODE_STATUS_FALSE, ErrorCode.ERR_CODE_PRECONDITION_FAILED_USER_NOT_EXIST, responseCommand);
        		} else {
        			
        			String appKey = null;
        			if(deviceType != null && "1".equals(deviceType)){ // Android
        				appKey = Global.getConfig("mob.sms.android.appkey");
        			} else if("2".equals(deviceType)){ // IOS
        				appKey = Global.getConfig("mob.sms.ios.appkey");
        			}
        			
        			String result = null;
        			try {
        				SmsVerifyKit verifyKit = new SmsVerifyKit(appKey, userName, Constant.SMS_DEFAULT_ZONE, code);
    					result = verifyKit.go();
    				} catch (Exception e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				}
        			
        			if(result != null && !"".equals(result)){
        				JSONObject json = new JSONObject(result);
        				int status = (Integer) json.get("status");
        				
        				if(status == 200){
        					
        					// 明文密码(通过DES解密)
        					String plainPassword;
        					try { // fOaniLvyTTE=  111111
        						plainPassword = DesUtil.decrypt(password, Constant.PASSWORD_DES_KEY);
        						
        						user.setPassword(SystemService.entryptPassword(plainPassword));
        						user.setUpdateDate(new Date());
        						user.setUpdateBy(user);
        						
        						// 保存用户信息
        						systemService.saveUser(user);
        						
        						return returnResult(ErrorCode.ERR_CODE_STATUS_TRUE, ErrorCode.ERR_CODE_SUCCESS, 
        				 				JsonConversion.writeMapJSON(resultMap), responseCommand);
        					} catch (Exception e) {
        						// TODO Auto-generated catch block
        						e.printStackTrace();
        					}
        				} else if(status == 468) {
        					logger.info("SMS Verify Code Status: "+Constant.SMS_VERIFY_CODE_STATUS_MAP.get(status));
        					return returnResult(ErrorCode.ERR_CODE_STATUS_FALSE, ErrorCode.ERR_CODE_PRECONDITION_FAILED_VERIFY_CODE, responseCommand);
        				} else {
        					logger.info("SMS Verify Code Status: "+Constant.SMS_VERIFY_CODE_STATUS_MAP.get(status));
        					return returnResult(ErrorCode.ERR_CODE_STATUS_FALSE, ErrorCode.ERR_CODE_PRECONDITION_FAILED_SMS_SERVICE, responseCommand);
        				}
        			}
        			
        		}
        	}
        	
        	return returnResult(ErrorCode.ERR_CODE_STATUS_FALSE, ErrorCode.ERR_CODE_EXCEPTION, 
	 				JsonConversion.writeMapJSON(resultMap), responseCommand);
        }
	}
	
	/**
	 * 个人中心-修改
	 * @param requestCommand
	 * @param responseCommand
	 * @return
	 * @throws JsonParseException
	 * @throws JsonMappingException
	 * @throws IOException
	 */
	public ResponseCommand userInfoModify(RequestCommand requestCommand, ResponseCommand responseCommand)
			throws JsonParseException, JsonMappingException, IOException {

		UserCommand userCommand = new UserCommand(requestCommand.getObject().toString());
		Map<String, Object> map = userCommand.getMap();
		String uid = (String) map.get("uid");
        String name = (String) map.get("name");
        String field = (String) map.get("field");
        String head = (String) map.get("head");
        
        Map<String,Object> resultMap = Maps.newHashMap();
        
        if(map.isEmpty() && StringUtils.isBlank(uid) && StringUtils.isBlank(field)){
            return returnResult(ErrorCode.ERR_CODE_STATUS_FALSE, ErrorCode.ERR_CODE_PRECONDITION_FAILED, responseCommand);
        } else {
        	
        	User user = systemService.getUser(uid);
        	
        	if(user == null){ // 用户不存在
        		return returnResult(ErrorCode.ERR_CODE_STATUS_FALSE, ErrorCode.ERR_CODE_PRECONDITION_FAILED_USER_NOT_EXIST, responseCommand);
        	} else {
        		
        		if(StringUtils.isNotBlank(field) && "name".equals(field)){
        			user.setName(name);
        		}
        		
        		if(StringUtils.isNotBlank(field) && "head".equals(field)){
        			user.setPhoto(head);
        		}
        		
        		user.setUpdateBy(user);
        		user.setUpdateDate(new Date());
        		
        		systemService.saveUser(user);
        	}
        }
		
        return returnResult(ErrorCode.ERR_CODE_STATUS_TRUE, ErrorCode.ERR_CODE_SUCCESS, 
 				JsonConversion.writeMapJSON(resultMap), responseCommand);
	}
	
	/**
	 * 手机验证码校验
	 * @param requestCommand
	 * @param responseCommand
	 * @return
	 * @throws JsonParseException
	 * @throws JsonMappingException
	 * @throws IOException
	 * @throws JSONException
	 */
	public ResponseCommand verifyCode(RequestCommand requestCommand, ResponseCommand responseCommand)
			throws JsonParseException, JsonMappingException, IOException, JSONException {

		UserCommand userCommand = new UserCommand(requestCommand.getObject().toString());
		Map<String, Object> map = userCommand.getMap();
		String userName = (String) map.get("userName");
        String deviceType = (String) map.get("deviceType");
        String code = (String) map.get("code");
        
        Map<String,Object> resultMap = Maps.newHashMap();
        
        if(map.isEmpty() && StringUtils.isBlank(userName) && StringUtils.isBlank(deviceType) && StringUtils.isBlank(code)){
            return returnResult(ErrorCode.ERR_CODE_STATUS_FALSE, ErrorCode.ERR_CODE_PRECONDITION_FAILED, responseCommand);
        } else {
        	
        	if(userName != null && !"".equals(userName)){
        			
    			String appKey = null;
    			if(deviceType != null && "1".equals(deviceType)){ // Android
    				appKey = Global.getConfig("mob.sms.android.appkey");
    			} else if("2".equals(deviceType)){ // IOS
    				appKey = Global.getConfig("mob.sms.ios.appkey");
    			}
    			
    			String result = null;
    			try {
    				SmsVerifyKit verifyKit = new SmsVerifyKit(appKey, userName, Constant.SMS_DEFAULT_ZONE, code);
					result = verifyKit.go();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
    			
    			if(result != null && !"".equals(result)){
    				JSONObject json = new JSONObject(result);
    				int status = (Integer) json.get("status");
    				
    				if(status == 200){
    					
    					return returnResult(ErrorCode.ERR_CODE_STATUS_TRUE, ErrorCode.ERR_CODE_SUCCESS, 
				 				JsonConversion.writeMapJSON(resultMap), responseCommand);
    				} else if(status == 468) {
    					System.out.println(Constant.SMS_VERIFY_CODE_STATUS_MAP.get(status));
    					return returnResult(ErrorCode.ERR_CODE_STATUS_FALSE, ErrorCode.ERR_CODE_PRECONDITION_FAILED_VERIFY_CODE, responseCommand);
    				} else {
    					System.out.println(Constant.SMS_VERIFY_CODE_STATUS_MAP.get(status));
    					return returnResult(ErrorCode.ERR_CODE_STATUS_FALSE, ErrorCode.ERR_CODE_PRECONDITION_FAILED_SMS_SERVICE, responseCommand);
    				}
    			}
    			
    		}
    	}
        
        return returnResult(ErrorCode.ERR_CODE_STATUS_TRUE, ErrorCode.ERR_CODE_SUCCESS, 
 				JsonConversion.writeMapJSON(resultMap), responseCommand);
	}
	
	/**
	 * 个人中心
	 * @param requestCommand
	 * @param responseCommand
	 * @return
	 * @throws JsonParseException
	 * @throws JsonMappingException
	 * @throws IOException
	 */
	public ResponseCommand userCenter(RequestCommand requestCommand, ResponseCommand responseCommand)
			throws JsonParseException, JsonMappingException, IOException {

		UserCommand userCommand = new UserCommand(requestCommand.getObject().toString());
		Map<String, Object> map = userCommand.getMap();
		String uid = (String) map.get("uid");
        
        Map<String,Object> resultMap = Maps.newHashMap();
        
        if(map.isEmpty() && StringUtils.isBlank(uid)){
            return returnResult(ErrorCode.ERR_CODE_STATUS_FALSE, ErrorCode.ERR_CODE_PRECONDITION_FAILED, responseCommand);
        } else {
        	
        	if(uid != null && !"".equals(uid)){
        		User user = userService.get(uid);
        		
        		if(user == null){ // 用户不存在
        			return returnResult(ErrorCode.ERR_CODE_STATUS_FALSE, ErrorCode.ERR_CODE_PRECONDITION_FAILED_USER_NOT_EXIST, responseCommand);
        		} else {
        			
        			// 用户分享统计
        			int userShareCount = 0;
        			UserShare usParam = new UserShare();
                    usParam.setDelFlag(Constant.DEL_FLAG_NORMAL);
                    usParam.setInviterId(uid);
                    long shareTotalCount = userShareService.findCount(usParam);
                    userShareCount = (new Long(shareTotalCount).intValue());
                     
                    resultMap.put("userShareCount", userShareCount);
                    
                    // 分润总额
                    int profitTotal = 0;
                    Map<String, Object> profitMap = Maps.newHashMap();
                    profitMap.put("DEL_FLAG_NORMAL", Constant.DEL_FLAG_NORMAL);
                    profitMap.put("userId", uid);
                    
                    Long profitTotalAmount = profitService.findTotalAmount(profitMap);
                    if(profitTotalAmount != null){
                    	profitTotal = (new Long(profitTotalAmount).intValue());
                    } 
                    
                    resultMap.put("profitTotal", profitTotal);
                    
                    // 银行卡统计
                    int bankCardCount = 0;
                    Map<String,Object> bankCardMap = Maps.newHashMap();
                    bankCardMap.put("DEL_FLAG_NORMAL", Constant.DEL_FLAG_NORMAL);
                    bankCardMap.put("userId", uid);
                    
                    bankCardCount = bankCardService.findCount(profitMap);
                    
                    resultMap.put("bankCardCount", bankCardCount);
                    
                    // 余额
                    int balanceTotal = 0;
                    resultMap.put("balanceTotal", balanceTotal);
                    
        		}
        	}
        }
        
        return returnResult(ErrorCode.ERR_CODE_STATUS_TRUE, ErrorCode.ERR_CODE_SUCCESS, 
 				JsonConversion.writeMapJSON(resultMap), responseCommand);
	}
	
        		
	/**
	 * 获取公司信息
	 * @return
	 */
	private Office getCompany(){
		
		Office company = null;
		
		Office cofficeParam = new Office();
		cofficeParam.setType("1"); // 公司
		cofficeParam.setParentIds("0");
		
		List<Office> cofficeList = officeService.findList(cofficeParam);
		if(cofficeList != null && cofficeList.size() > 0){
			company = new Office(cofficeList.get(0).getId());
		} else {
			company = new Office("1");
		}
		
		return company;
	}
	
	/**
	 * 获取部门信息
	 * @return
	 */
	private Office getOffice(){
		
		Office office = null;
		
		Office oofficeParam = new Office();
		oofficeParam.setType("2"); // 部门
		oofficeParam.setParentIds("0");
		List<Office> oofficeList = officeService.findList(oofficeParam);
		if(oofficeList != null && oofficeList.size() > 0){
			office = new Office(oofficeList.get(0).getId());
		} else {
			office = new Office("2");
		}
		
		return office;
	}
	
	/**
	 * 获取角色列表
	 * @return
	 */
	private List<Role> getRoleListByEn(String enname){
		
		List<Role> roleList = Lists.newArrayList();
		
		Role roleParam = new Role();
		roleParam.setEnname(enname); // 普通用户
		roleList = systemService.findRole(roleParam);
		
		return roleList;
	}
	
}
